package com.qianjiangcloud.store.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qianjiangcloud.store.Run;
import com.qianjiangcloud.store.activity.AgentActivity;
import com.qianjiangcloud.store.adapter.ShowGoodsRecycleViewAdapter;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import com.qianjiangcloud.store.R;
import com.qianjiangcloud.store.libary.cache.MyImageLoader;

/**
 * 商品展示栏
 */
public class ShowGoodsUtils {
    private  RelativeLayout adsFrameLayout=null;
    private  View vi=null;
    private  Context context;
    private RecyclerView mRecyclerView;
    private DataAdapter mDataAdapter;
    private int mCurrentCounter;
    private ShowGoodsRecycleViewAdapter mShowGoodsRecycleViewAdapter;
    public View getShowGoodsViews(Context mContext){
        context = mContext;
        //创建布局
        adsFrameLayout = new RelativeLayout(mContext);
        RelativeLayout.LayoutParams adsFrameLayoutParams= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        adsFrameLayout.setLayoutParams(adsFrameLayoutParams);
        vi = LayoutInflater.from(mContext).inflate(R.layout.show_goods_view, null);
        //创建布局
        mRecyclerView = (RecyclerView) vi.findViewById(R.id.goods_recyclerview_grid);
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(mContext, 2);
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        //设置分割线
        DividerGridItemDecoration dividerGridItemDecorationUtils = new DividerGridItemDecoration(context);
        mRecyclerView.addItemDecoration(dividerGridItemDecorationUtils );
        adsFrameLayout.addView(vi);
        return adsFrameLayout;
    }


    public void setUpview(String json_str,Context mContext,View view,String title,String title_color,String title_icon,String isshow_module_title){
        //判断是否有更多的头部布局
        RelativeLayout isshow_title = (RelativeLayout) view.findViewById(R.id.isshow_title);
        if(isshow_module_title.equals("0")){
            isshow_title.setVisibility(View.GONE);
        }
        TextView title_name= (TextView) view.findViewById(R.id.title);
        ImageView icon= (ImageView) view.findViewById(R.id.image_icon);
        ImageView icon2= (ImageView) view.findViewById(R.id.image_icon2);
        ImageView title_image= (ImageView) view.findViewById(R.id.image);
        if(null!=title){
            title_name.setText(title);
        }
        if(null!=title_color){
            title_name.setTextColor(Color.parseColor(title_color));
            icon.setBackgroundColor(Color.parseColor(title_color));
            icon2.setBackgroundColor(Color.parseColor(title_color));
        }
        if(null!=title_icon){
            MyImageLoader.displayDefaultImage(title_icon,title_image);
        }
        this.mRecyclerView= (RecyclerView) view.findViewById(R.id.goods_recyclerview_grid);
        parseHomeAdsJson(json_str ,mContext);
    }

    private void notifyDataSetChanged() {
        mShowGoodsRecycleViewAdapter.notifyDataSetChanged();
    }

    private void addItems(ArrayList<JSONObject> list) {
        mDataAdapter.addAll(list);
        mCurrentCounter += list.size();
    }
    private void parseHomeAdsJson(String json_str, Context mContext) {
        ArrayList<JSONObject>  mShowGoodsArray = new ArrayList<JSONObject>();
//    private void notifyDataSetChanged() {
//        mShowGoodsRecycleViewAdapter.notifyDataSetChanged();
//    }
//
//    private void addItems(ArrayList<JSONObject> list) {
//        mDataAdapter.addAll(list);
//        mCurrentCounter += list.size();
//    }
//    private void parseHomeAdsJson(String json_str, Context mContext, ArrayList<JSONObject> mShowGoodsArray) {
//
        try {
            JSONObject all = new JSONObject(json_str);
            if (Run.checkRequestJson(mContext, all)) {
                JSONObject infoObject=all.getJSONObject("data");
                JSONArray child = infoObject.optJSONArray("info");
                if (child != null && child.length() > 0) {
                    mShowGoodsArray.clear();
                    for (int i = 0; i < child.length(); i++) {
                        JSONObject topJson = child.getJSONObject(i);
                        mShowGoodsArray.add(topJson);
                    }
                    if (mShowGoodsArray.size() > 0) {
                        reloadMainView(false,mContext,mShowGoodsArray);
                    } else {
                        reloadMainView(true,mContext,mShowGoodsArray);
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void reloadMainView(boolean isOutDate, Context mContext, ArrayList<JSONObject>  mShowGoodsArray ) {
        if (mShowGoodsArray != null && mShowGoodsArray.size() > 0 && !isOutDate) {
             mDataAdapter = new DataAdapter(mContext);
            mDataAdapter.addAll(mShowGoodsArray);
//             mShowGoodsRecycleViewAdapter = new ShowGoodsRecycleViewAdapter(mContext,mDataAdapter,mShowGoodsArray);
//            ShowGoodsRecycleViewAdapter mShowGoodsRecycleViewAdapter = new ShowGoodsRecycleViewAdapter(mContext,mShowGoodsArray);

            mRecyclerView.setAdapter(mDataAdapter);
        } else {
            adsFrameLayout.setVisibility(View.GONE);
        }
    }

    private class DataAdapter extends RecyclerView.Adapter {

        private LayoutInflater mLayoutInflater;
        private ArrayList<JSONObject> mDataList = new ArrayList();

        public DataAdapter(Context context) {
            mLayoutInflater = LayoutInflater.from(context);
        }

        private void addAll(ArrayList<JSONObject> list) {
            int lastIndex = this.mDataList.size();
            if (this.mDataList.addAll(list)) {
                notifyItemRangeInserted(lastIndex, list.size());
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(mLayoutInflater.inflate(R.layout.show_goods_item, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            JSONObject item = mDataList.get(position);

            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.textView.setText(item.optString("good_title"));
            viewHolder.cost_price.setText("￥"+ item.optString("cost_price"));
            viewHolder.pay_count.setText(item.optString("pay_count")+"人付款");
            viewHolder.sale_price.setText(item.optString("sale_price"));
            viewHolder.cost_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰
           /* Uri imageUri = Uri.parse(item.optString("good_img"));
            viewHolder.iv.setImageURI(imageUri);*/
            MyImageLoader.displayDefaultImage(item.optString("good_img"), viewHolder.iv);

        }

        @Override
        public int getItemCount() {
            return mDataList.size();
        }

        private class ViewHolder extends RecyclerView.ViewHolder {

            private TextView textView;
            private LinearLayout ll;
            private ImageView iv;
            private TextView sale_price;
            private TextView cost_price;
            private TextView pay_count;
            public ViewHolder(final View itemView) {
                super(itemView);
                 ll = (LinearLayout) itemView.findViewById(R.id.ll_show_goods_item);
                 textView= (TextView) itemView.findViewById(R.id.goods_gallery_item_text);
                sale_price= (TextView) itemView.findViewById(R.id.sale_price);
                cost_price= (TextView) itemView.findViewById(R.id.cost_price);
                pay_count= (TextView) itemView.findViewById(R.id.pay_count);
                    iv = (ImageView)itemView.findViewById(R.id.goods_show_item_image);
                ll.setOnClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                       String text = mDataList.get(RecyclerViewUtils.getAdapterPosition(mRecyclerView, ViewHolder.this)).optString("good_title");
//                        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
                        JSONObject obj=mDataList.get(RecyclerViewUtils.getAdapterPosition(mRecyclerView, ViewHolder.this));
                        if (obj==null)
                        {
                            return;
                        }
//                        Log.i("intent",obj+"");
                        String goodsIID = obj.optString("goods_id");
                        Intent intent = AgentActivity.intentForFragment(
                                itemView.getContext(), AgentActivity.FRAGMENT_GOODS_DETAIL)
                                .putExtra(Run.EXTRA_CLASS_ID, goodsIID);
                        itemView.getContext().startActivity(intent);

                    }
                });
            }
        }
    }

}
